import type { IActivityTicketResult } from "@/api/activity/types";
import { RefundTicketTypeEnum, TicketBuyLimitEnum, TicketBuyTimeTypeEnum, TicketStatusEnum, TicketStockLimitEnum } from "@/enums/activityStatusEnum";
import { isNullOrUnDef } from "@/utils/is";
import { formatMoney, type FormatMoneyOptions } from "@/utils/number";
import { formatToDateTime } from '@/utils/dateUtil';

/**
 * @description 购票时间描述
 * @date 26/06/2024
 * @export
 * @param {IActivityTicketResult} item
 */
export function getPurchaseTimeDes(item?: IActivityTicketResult) {
  if (item?.buyTimeType === TicketBuyTimeTypeEnum.ACTIVITY_END_TIME) {
    return '活动结束前可报名'
  }

  return `${formatToDateTime(item?.startTime, 'YYYY年MM月DD日 HH:mm')} ~ ${formatToDateTime(item?.endTime, 'YYYY年MM月DD日 HH:mm')}`
}

/**
 * @description 退票说明
 * @date 26/06/2024
 * @export
 * @param {IActivityTicketResult} item
 */
export function getRefundTimeDes(item?: IActivityTicketResult) {
  if (item?.refundTicketType === RefundTicketTypeEnum.CUSTOM_TIME) {
    return `活动开始前${item.refundTicketHour}小时内不允许申请取消报名`
  }

  return '活动结束后24小时内未签到核销可申请取消报名'
}

/**
 * @description 获取当前用户对应的票种价格
 * @date 26/06/2024
 * @export
 * @param {IActivityTicketResult} [item]
 */
export function getRealPrice(item?: IActivityTicketResult, options: Omit<FormatMoneyOptions, 'money'> = {}) {
  const price = isNullOrUnDef(item?.discountPrice) ? item?.price : item?.discountPrice

  return formatMoney({ money: price || 0, ...options })
}

/**
 * @description 获取最大购买数
 * @date 27/06/2024
 * @export
 * @param {IActivityTicketResult} item
 */
export function getBuyMaxNum(item: IActivityTicketResult) {
  const { buyMaxNum, buyLimit, stockNum, stockLimit, ticketStatus } = item
  if (ticketStatus !== TicketStatusEnum.RUSH_TICKET) return 1;
  if (buyLimit === TicketBuyLimitEnum.LIMIT) return buyMaxNum || 1;
  if (stockLimit === TicketStockLimitEnum.LIMIT) return stockNum || 1;
  return void 0;
}